<!doctype html>
<html lang="en">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>Swagger UI</title>

    <link href="dist/css/style.css" rel="stylesheet">
    <!-- <link rel="icon" href="https://getbootstrap.com/favicon.ico"> -->
</head>

<body data-spy="scroll" data-target="#resources_nav" data-offset="60">
    <nav id="main-nav" class="navbar navbar-expand-md navbar-dark bg-dark">
        <a class="navbar-brand" href="">Swagger UI</a>

        <form id='api_selector' class="form-inline ml-auto d-none d-lg-block">
            <div class="input">
                <select class="parameter" id="select_baseUrl" name="select_baseUrl"></select>
            </div>
            <!-- <div class="input"><input placeholder="http://example.com/api" id="input_baseUrl" name="baseUrl" type="text" />
            </div> -->
        </form>
    </nav>
    <main role="main" class="container-fluid">
        <div id="swagger-ui-container" class="row"></div>
    </main>

    <!-- Dependencies -->
    <script src="dist/lib/jquery-3.2.1.min.js" type="text/javascript"></script>
    <script src="dist/lib/jquery.wiggle.min.js" type="text/javascript"></script>
    <script src="dist/lib/handlebars-2.0.0.js" type="text/javascript"></script>
    <script src="dist/lib/underscore-min.js" type="text/javascript"></script>
    <script src="dist/lib/backbone-min.js" type="text/javascript"></script>
    <script src="dist/lib/marked.js" type="text/javascript"></script>
    <script src="dist/lib/bootstrap.min.js" type="text/javascript"></script>
    <script src="dist/lib/highlight.pack.js" type="text/javascript"></script>
    <script src="dist/lib/scrollPosStyler.min.js" type="text/javascript"></script>
    <script src="dist/lib/fontawesome-all.min.js" type="text/javascript"></script>
    <!--<script src="dist/lib/swagger-oauth.js" type="text/javascript"></script>-->
    <script src="dist/swagger-ui.js" type="text/javascript"></script>

    <script type="text/javascript">

        $(function() {

            var springfox = {
                "baseUrl": function() {
                    var urlMatches = /(.*)\/swagger-ui\/index.html.*/.exec(window.location.href);
                    return urlMatches[1];
                },
                "securityConfig": function(cb) {
                    $.getJSON(this.baseUrl() + "/swagger-resources/configuration/security", function(data) {
                        cb(data);
                    });
                },
                "uiConfig": function(cb) {
                    $.getJSON(this.baseUrl() + "/swagger-resources/configuration/ui", function(data) {
                        cb(data);
                    });
                }
            };

            window.springfox = springfox;
//            window.oAuthRedirectUrl = springfox.baseUrl() + './dist/o2c.html'

            window.springfox.uiConfig(function(data) {

                window.swaggerUi = new SwaggerUi({
                    url: '/v2/api-docs', // Change to your json
                    dom_id: "swagger-ui-container",
                    supportedSubmitMethods: ['get', 'post', 'put', 'delete', 'patch', 'head', 'options'],
                    onComplete: function(swaggerApi, swaggerUi) {

                        $('pre code')
                            .each(function(i, e) {
                                hljs.highlightBlock(e)
                            });

                        ScrollPosStyler.init({ scrollOffsetY: 54 });

                        // Scroll To Item
                        var ancloc = window.location.hash;
                        if (ancloc) {
                            document.location.href = ancloc;
                        }

                        // Set Key by default
                        addApiKeyAuthorization();

                        $('#select_baseUrl').change(function() {

                            window.swaggerUi.updateSwaggerUi({
                                url: $('#select_baseUrl').val()
                            });
                         });

                    },
                    onFailure: function(data) {
                        console.log("Unable to Load SwaggerUI");
                    },
                    docExpansion: "none",
                    sorter: "alpha"
                });

                window.swaggerUi.load();

                function addApiKeyAuthorization() {
                    var authToken = JSON.parse(localStorage.getItem("jhi-authenticationtoken") || sessionStorage.getItem("jhi-authenticationtoken"));
                    var apiKeyAuth = new SwaggerClient.ApiKeyAuthorization("Authorization", "Bearer " + authToken, "header");
                    window.swaggerUi.api.clientAuthorizations.add("bearer", apiKeyAuth);
                }

                initializeBaseUrl();

            });

            function maybePrefix(location, withRelativePath) {
                var pat = /^https?:\/\//i;
                if (pat.test(location)) {
                    return location;
                }
                return withRelativePath + location;
            }

            function initializeBaseUrl() {
                var relativeLocation = springfox.baseUrl();

                // $('#input_baseUrl').hide();

                $.getJSON(relativeLocation + "/swagger-resources", function(data) {

                    var $urlDropdown = $('#select_baseUrl');
                    $urlDropdown.empty();
                    $.each(data, function(i, resource) {
                        var option = $('<option></option>')
                            .attr("value", maybePrefix(resource.location, relativeLocation))
                            .text(resource.name + " (" + resource.location + ")");
                        $urlDropdown.append(option);
                    });
                    $urlDropdown.change();
                });
            }

        });

    </script>
</body>

</html>
